package com.yx1996.web.servlet;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yx1996.domain.Customer;
import com.yx1996.service.CustomerService;
import com.yx1996.serviceimpl.CustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;


public class AddCustomerServlet extends HttpServlet {




    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        //1 获得参数并封装到Customer对象
        Customer c = new Customer();
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            BeanUtils.populate(c,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2 调用Service保存客户
        customerService.save(c);
        //3 重定向到客户列表
        response.sendRedirect(request.getContextPath() + "/menu.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
